package com.googlecode.bip.xml.importe;

import java.sql.SQLException;
import javax.naming.NamingException;

/**
 *
 * @author Claude CHOISNET
 */
class SQLValueDescription  extends Commiter implements Commitable
{
    private SQLQuestion         parentSQLQuestion;
    private SQLQuestionLocal    parentSQLQuestionLocal;
    private Integer             value;
    private String              valuedescription;

    SQLValueDescription()
    {
    }

    void setParent( SQLQuestion parentSQLQuestion )
    {
        if( parentSQLQuestion == null ) {
            throw new NullPointerException( "parent SQLQuestion is null" );
            }
        this.parentSQLQuestion = parentSQLQuestion;
    }

    public void setParent( SQLQuestionLocal parentSQLQuestionLocal )
    {
        if( parentSQLQuestionLocal == null ) {
            throw new NullPointerException( "parent SQLQuestionLocal is null" );
            }
        this.parentSQLQuestionLocal = parentSQLQuestionLocal;
    }

    void setValue( int value )
    {
        this.value = value;
    }

    void setValueDescription( String description )
    {
        if( description == null ) {
            throw new NullPointerException( "description is null" );
            }

        this.valuedescription = description;
    }

    @Override
    public void commit() throws NamingException, SQLException
    {
        if( value == null ) {
            throw new NullPointerException( "value is null" );
            }
        if( valuedescription == null ) {
            throw new NullPointerException( "valuedescription is null" );
            }

        String sql = "REPLACE INTO `questionvalues_local` (`idquestion`, `idlocal`, `value`, `valuedescription`) VALUES ('"
            + parentSQLQuestion.getIdQuestion()
            + "','"
            + parentSQLQuestionLocal.getIdLocal()
            + "','"
            + value
            + "','"
            + valuedescription
            + "');";

        super.performeCommit( sql );
    }

}
